﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PolePositionMobileUtilityEntity;
using PolePositionMobileUtilityGeneral.Events;
using PolePositionMobileUtilityLogic;

namespace PolePositionMobileUtilityApplication.Controls
{
    public partial class HomeGrid : UserControl
    {
        private Int32 m_SelectedId = Int32.MinValue;

        public HomeGrid()
        {
            InitializeComponent();
        }

        public void ConfigureGrid()
        {
            grdInspections.RowHeadersVisible = false;
            grdInspections.DataSource = LUtilityInspection.GetMainGrid();
            #region Set grid styles
            DataGridTableStyle _GridTableStyle = new DataGridTableStyle();
            DataGridTextBoxColumn _DGCol;
            #region InspectionId
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "Result_Id";
            _DGCol.Width = 0;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion

            #region InspectionType
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "InspectionType_Name";
            _DGCol.HeaderText = "Insp.";
            _DGCol.Width = 30;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion
            
            #region Result
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "ResultType_Name";
            _DGCol.HeaderText = "Result";
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion

            #region Top
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "Toptype_Name";
            _DGCol.HeaderText = "Top";
            _DGCol.NullText = "";
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion

            #region Number
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "NumberPosition";
            _DGCol.HeaderText = "Asset Number";
            _DGCol.Width = 100;
            _DGCol.NullText = "";
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion
            grdInspections.TableStyles.Add(_GridTableStyle);
            #endregion
        }

        public void ReloadGrid()
        {
            grdInspections.DataSource = LUtilityInspection.GetMainGrid();
        }

        public Int32 GetSelectedInspection()
        {
            if (grdInspections.CurrentRowIndex >= 0 && grdInspections.IsSelected(grdInspections.CurrentRowIndex))
            {
                m_SelectedId = (Int32)grdInspections[grdInspections.CurrentRowIndex, 0];
            }
            else
            {
                m_SelectedId = Int32.MinValue;
            }
            return m_SelectedId;

        }
        public Int32 GetLastInspection()
        {
            if (grdInspections.VisibleRowCount > 0)
            {
                m_SelectedId = (Int32)grdInspections[grdInspections.VisibleRowCount - 1, 0];
            }
            else
            {
                m_SelectedId = Int32.MinValue;
            }
            return m_SelectedId;
        }

        private void grdInspections_CurrentCellChanged(object sender, EventArgs e)
        {
            if (grdInspections.CurrentRowIndex >= 0)
            {
                Rectangle r = grdInspections.GetCellBounds(grdInspections.CurrentRowIndex, 4); 
                grdInspections.Select(grdInspections.CurrentRowIndex);
                menuGrid.Show(grdInspections, new Point(r.X, r.Y));
            }

        }

        #region HomeGridEvents
        
        public event EventHandler<GridEventArgs> ClickedAddImage;
        public event EventHandler<GridEventArgs> ClickedDelete;
        public event EventHandler<GridEventArgs> ClickedEdit;
        public event EventHandler<GridEventArgs> ClickedDuplicate;
        private void OnClickedAddImage(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddImage != null)
            {
                ClickedAddImage(o, e);
            }
        }
        private void OnClickedDelete(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }
        private void OnClickedEdit(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedDuplicate(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDuplicate != null)
            {
                ClickedDuplicate(o, e);
            }
        }

        private void mitDuplicate_Click(object sender, EventArgs e)
        {
            OnClickedDuplicate(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitEdit_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitAddImage_Click(object sender, EventArgs e)
        {
            OnClickedAddImage(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitDelete_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, new GridEventArgs(GetSelectedInspection()));
        }

        #endregion




    }
}
